package view.views.dialogContentViews;

import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.Constants;
import view.components.BinaryIpTextArea;
import view.components.IPTextArea;
import view.components.SubnetComboBox;
import view.listeners.BinaryToDecimalListener;
import view.listeners.DecimalToBinaryListener;
import view.listeners.RadioButtonListener;

/**
 * @author Alexander Müller
 * 
 */
public class AddSimpleIPContentPanel extends JPanel {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final SubnetComboBox subnetBox;
	private final IPTextArea ipTextArea;
	private final BinaryIpTextArea binaryIpTextArea;
	private final JTextField textField;
	private final ArrayList<JRadioButton> buttons = new ArrayList<JRadioButton>();

	public AddSimpleIPContentPanel() {

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel ipLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel choosePanel = new JPanel(ipLabelPanel.getLayout());
		JPanel ipPanel = new JPanel(ipLabelPanel.getLayout());
		JPanel binaryIpPanel = new JPanel(ipLabelPanel.getLayout());
		JPanel textLabelPanel = new JPanel(ipLabelPanel.getLayout());
		JPanel textPanel = new JPanel(ipLabelPanel.getLayout());
		JPanel boxLabelPanel = new JPanel(ipLabelPanel.getLayout());
		JPanel boxPanel = new JPanel(ipLabelPanel.getLayout());

		JLabel ipLabel = new JLabel(Constants.ADDIPLABEL);

		ipLabelPanel.add(ipLabel);

		JRadioButton decimal = new JRadioButton(Constants.DECIMAL);
		decimal.setSelected(true);
		decimal.addActionListener(new RadioButtonListener(decimal, this));
		buttons.add(decimal);
		JRadioButton binary = new JRadioButton(Constants.BINARY);
		binary.addActionListener(new RadioButtonListener(binary, this));
		buttons.add(binary);
		ButtonGroup group = new ButtonGroup();
		group.add(decimal);
		group.add(binary);

		choosePanel.add(decimal);
		choosePanel.add(binary);

		ipTextArea = new IPTextArea();

		ipPanel.add(ipTextArea);

		binaryIpTextArea = new BinaryIpTextArea();
		for (int j = 0; j < binaryIpTextArea.getTextList().size(); j++) {
			binaryIpTextArea.getTextList().get(j).setEditable(false);
			if (j < 3) {
				binaryIpTextArea.getDotList().get(j)
						.setBackground(this.getBackground());

			}
		}

		binaryIpPanel.add(binaryIpTextArea);

		addConverterToBinary(ipTextArea);
		addConverterToDecimal(binaryIpTextArea);

		JLabel textLabel = new JLabel(Constants.ADDIPTEXT);

		textLabelPanel.add(textLabel);

		textField = new JTextField(20);
		textField.setBorder(null);

		textPanel.add(textField);

		JLabel boxLabel = new JLabel(Constants.CHOOSESUBNET);
		boxLabelPanel.add(boxLabel);

		subnetBox = new SubnetComboBox();

		boxPanel.add(subnetBox);

		this.add(ipLabelPanel);
		this.add(choosePanel);
		this.add(ipPanel);
		this.add(binaryIpPanel);
		this.add(textLabelPanel);
		this.add(textPanel);
		this.add(boxLabelPanel);
		this.add(boxPanel);

	}

	private void addConverterToDecimal(BinaryIpTextArea binaryIpText) {
		for (JTextField element : binaryIpText.getTextList()) {
			element.addFocusListener(new BinaryToDecimalListener(this,
					binaryIpText.getTextList().indexOf(element)));
		}

	}

	private void addConverterToBinary(IPTextArea ipText) {
		for (JTextField element : ipText.getTextList()) {
			element.addFocusListener(new DecimalToBinaryListener(this, ipText
					.getTextList().indexOf(element)));
		}

	}

	public SubnetComboBox getSubnetBox() {
		return subnetBox;
	}

	public IPTextArea getIpTextArea() {
		return ipTextArea;
	}

	public JTextField getTextField() {
		return textField;
	}

	public BinaryIpTextArea getBinaryIpTextArea() {
		return binaryIpTextArea;
	}

	public ArrayList<JRadioButton> getButtons() {
		return buttons;
	}

}
